package com.xhf.tools;

import java.lang.management.*;

public class JvmMonitoringExample {
    public static void main(String[] args) throws Exception {
        // 获取Java 虚拟机管理器
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        
        // 获取JVM的名称
        String jvmName = runtimeMXBean.getName();
        System.out.println("JVM名称: " + jvmName);
        
        // 获取JVM的启动时间
        long startTime = runtimeMXBean.getStartTime();
        System.out.println("JVM启动时间: " + startTime);
        
        // 获取JVM的运行时信息
        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
        
        // 获取操作系统名称
        String osName = osMXBean.getName();
        System.out.println("操作系统名称: " + osName);
        
        // 获取CPU核心数
        int availableProcessors = osMXBean.getAvailableProcessors();
        System.out.println("CPU核心数: " + availableProcessors);
        
        // 获取内存使用情况
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

        
        // 获取堆内存使用情况
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        System.out.println("堆内存使用情况: " + heapMemoryUsage);
        
        // 获取非堆内存使用情况
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        System.out.println("非堆内存使用情况: " + nonHeapMemoryUsage);

        // 获取线程使用情况
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
        System.out.println("死锁线程IDS: " + deadlockedThreads);

    }
}